export function openWeb(url, title, type = 0) {
  //type 0新打开标签页 1新打开子窗口
  window.prompt(
    "",
    `up://["open.web",${Date.now()},{"url":"${url}","title":"${title}","popuptype":${type}}]`
  );
}

export function imgPath(data) {
  const reg = /<img [^>]*src=['"]([^'"]+)[^>]*>/gi;
  var regExp = window.location.href;
  let urlPath = "";
  switch (true) {
    case /http:\/\/app-test.zx093.com/.test(regExp):
      urlPath = "http://product-test.zx093.com";
      break;
    case /http:\/\/app-dev.zx093.com/.test(regExp):
      urlPath = "http://product-dev.zx093.com";
      break;
    case /http:\/\/client.zx093.com/.test(regExp):
      urlPath = "http://product.zx093.com";
      break;
  }
  return data.replace(reg, (img, url) =>
    url.indexOf("http") != -1
      ? `<img src="${url}" onerror="this.src='${url}'"/>`
      : `<img src="${urlPath + url}" onerror="javascript:this.src='${urlPath + url}'"/>`
  );
}

export function objToParamStr(paramObj) {
  const filter = function(str) {
    // 特殊字符转义
    str += ""; // 隐式转换
    str = str.replace(/%/g, "%25");
    str = str.replace(/\+/g, "%2B");
    str = str.replace(/ /g, "%20");
    str = str.replace(/\//g, "%2F");
    str = str.replace(/\?/g, "%3F");
    str = str.replace(/&/g, "%26");
    str = str.replace(/\=/g, "%3D");
    str = str.replace(/#/g, "%23");
    return str;
  };
  const sdata = [];
  for (let attr in paramObj) {
    sdata.push(`${attr}=${filter(paramObj[attr])}`);
  }
  return sdata.join("&");
}

export function Timer(fun, time = 1000) {
  var jishiqi;
  var step = function(timestamp) {
    setTimeout(() => {
      jishiqi = window.requestAnimationFrame(step);
      fun(jishiqi);
    }, time);
  };
  jishiqi = window.requestAnimationFrame(step);
  fun(jishiqi);
}
